/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bookshop.managedBean;

import com.bookshop.entity.Customer;
import com.bookshop.service.SecurityService;
import javax.ejb.EJB;
import javax.faces.event.ActionEvent;
import org.primefaces.context.RequestContext;

/**
 *
 * @author Thibault
 */
public class SecurityManagedBean {
    
    @EJB
    private SecurityService securityService;

    private Customer customer = new Customer();
    private boolean loggedIn = false;
    private boolean admin = false;

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public boolean isLoggedIn() {
        return loggedIn;
    }

    public void setLoggedIn(boolean loggedIn) {
        this.loggedIn = loggedIn;
    }

    public boolean isAdmin() {
        return admin;
    }

    public void setAdmin(boolean admin) {
        this.admin = admin;
    }
    
    /**
     * Creates a new instance of SecurityManagedBean
     */
    public SecurityManagedBean() {
    }
    
    public String logInClickEvent(String login, String password) {
        Customer c = securityService.findCustomerByLoginAndPassword(login, password);
        if (c != null) {
            this.setCustomer(c);
            this.setLoggedIn(true);
            if (c.getLogin().equals("admin")) {
                this.setAdmin(true);
            }
            return "toHome";
        }
        return "toLogIn";
//        RequestContext context = RequestContext.getCurrentInstance();         
//        context.addCallbackParam("loggedIn", this.isLoggedIn()); 
    }
    
    public String logOutClickEvent() {
        this.setCustomer(new Customer());
        this.setLoggedIn(false);
        this.setAdmin(false);
        return "toHome";
    }
    
}
